package com.yuju.controller.accounts;

import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yuju.account.enums.UnitAccountBookReturnCodeEnum;
import com.yuju.account.enums.UnitAccountSettleEnum;
import com.yuju.account.model.UnitAccountBookModel;
import com.yuju.account.service.IUnitAccountBookService;
import com.yuju.common.PageModel;
import com.yuju.common.Result;
import com.yuju.common.fileld.validator.FieldValidator;
import com.yuju.controller.base.BaseController;
import com.yuju.enums.ReturnStatusEnum;
import com.yuju.util.Utils;
import com.yuju.vo.account.UnitAccountBookVO;

/**
 * 类名：UnitAccountBookController  <br />
 *
 * 功能：
 *
 * @author : jiaokn <br />
 * @Date : 2017年11月27日 下午1:56:14  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("unitAccountBook")
public class UnitAccountBookController extends BaseController<UnitAccountBookVO, UnitAccountBookModel> {
	
	@Autowired
	IUnitAccountBookService unitAccountBookService;
	
	/**
	 * 功能：取消结账<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年12月27日 下午4:38:54 <br/>
	 */
	@RequestMapping("reduce")
	@ResponseBody
	public Result reduce(@RequestBody JSONObject obj){
		UnitAccountBookVO unitAccountBookVO = obj.toJavaObject(UnitAccountBookVO.class);
		unitAccountBookVO.setUpdateUserId(unitAccountBookVO.getEmpId());
		int i = unitAccountBookService.updateSettle(convert(unitAccountBookVO));
		Result result = new Result();
		if(i>0){
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(UnitAccountBookReturnCodeEnum.UNITACCOUNTBOOK_REDUCE_SUCCESS.getCode());
			result.setReturnMsg(UnitAccountBookReturnCodeEnum.UNITACCOUNTBOOK_REDUCE_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(UnitAccountBookReturnCodeEnum.UNITACCOUNTBOOK_REDUCE_FAILURE.getCode());
			result.setReturnMsg(UnitAccountBookReturnCodeEnum.UNITACCOUNTBOOK_REDUCE_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 功能：查询帐套列表 并总结账务<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年12月15日 下午12:00:02 <br/>
	 */
	@RequestMapping("listAadSum")
	@ResponseBody
	public Result listAadSum(@RequestBody JSONObject obj){
		UnitAccountBookVO unitAccountBookVO = obj.toJavaObject(UnitAccountBookVO.class);
		Map<String, String> fail = FieldValidator.validateAll(unitAccountBookVO, "pager", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		PageModel pageModel = unitAccountBookService.listAndSum(convert(unitAccountBookVO), unitAccountBookVO.getPage(), unitAccountBookVO.getPageSize());
		Result result = new Result();
		if(Utils.isNotNull(pageModel) && pageModel.getTotal()>0){
			result.setPageModel(pageModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(UnitAccountBookReturnCodeEnum.UNITACCOUNTBOOK_LIST_SUCCESS.getCode());
			result.setReturnMsg(UnitAccountBookReturnCodeEnum.UNITACCOUNTBOOK_LIST_SUCCESS.getDesc());
		}else if(Utils.isNotNull(pageModel) && (pageModel.getTotal()<=0 || Utils.isNotNull(pageModel.getTotal()))){
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(UnitAccountBookReturnCodeEnum.UNITACCOUNTBOOK_NOTHING.getCode());
			result.setReturnMsg(UnitAccountBookReturnCodeEnum.UNITACCOUNTBOOK_NOTHING.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(UnitAccountBookReturnCodeEnum.UNITACCOUNTBOOK_LIST_FAILURE.getCode());
			result.setReturnMsg(UnitAccountBookReturnCodeEnum.UNITACCOUNTBOOK_LIST_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 功能：新增帐套<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年12月19日 下午4:53:55 <br/>
	 */
	@RequestMapping("save")
	@ResponseBody
	public Result save(@RequestBody JSONObject obj){
		UnitAccountBookVO unitAccountBookVO = obj.toJavaObject(UnitAccountBookVO.class);
		unitAccountBookVO.setIsSettle(UnitAccountSettleEnum.FALSE.getValue());
		unitAccountBookVO.setCreateUserId(unitAccountBookVO.getEmpId());
		JSONArray json = obj.getJSONArray("unitAccountIdList");
		String jString = json.toJSONString();
		ArrayList<String> unitAccountIdList = JSON.parseObject(jString,new TypeReference<ArrayList<String>>(){});
		int i = unitAccountBookService.saveUnitAccountBook(convert(unitAccountBookVO), unitAccountIdList);
		Result result = new Result();
		if(i>0){
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(UnitAccountBookReturnCodeEnum.UNITACCOUNTBOOK_SAVE_SUCCESS.getCode());
			result.setReturnMsg(UnitAccountBookReturnCodeEnum.UNITACCOUNTBOOK_SAVE_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(UnitAccountBookReturnCodeEnum.UNITACCOUNTBOOK_SAVE_FAILURE.getCode());
			result.setReturnMsg(UnitAccountBookReturnCodeEnum.UNITACCOUNTBOOK_SAVE_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 功能：单位帐套编辑接口<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年12月11日 上午11:21:19 <br/>
	 */
	@RequestMapping("update")
	@ResponseBody
	public Result update(@RequestBody JSONObject obj){
		UnitAccountBookVO unitAccountBookVO = obj.toJavaObject(UnitAccountBookVO.class);
		unitAccountBookVO.setUpdateUserId(unitAccountBookVO.getEmpId());
		unitAccountBookVO.setGmtModified(new Date());
		JSONArray json = obj.getJSONArray("unitAccountIdList");
		String jString = json.toJSONString();
		ArrayList<String> unitAccountIdList = JSON.parseObject(jString,new TypeReference<ArrayList<String>>(){});
		int i = unitAccountBookService.updateUnitAccountBook(convert(unitAccountBookVO), unitAccountIdList);
		Result result = new Result();
		if(i>0){
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(UnitAccountBookReturnCodeEnum.UNITACCOUNTBOOK_UPDATE_SUCCESS.getCode());
			result.setReturnMsg(UnitAccountBookReturnCodeEnum.UNITACCOUNTBOOK_UPDATE_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(UnitAccountBookReturnCodeEnum.UNITACCOUNTBOOK_UPDATE_FAILURE.getCode());
			result.setReturnMsg(UnitAccountBookReturnCodeEnum.UNITACCOUNTBOOK_UPDATE_FAILURE.getDesc());
		}
		return result;
	}
	
}
